<template>
	<view>
		<tui-row style="height: 10%;">
			<div style="display: flex;align-items: center;justify-content: center;">
				<image src="/static/condition.png" style="height: 50rpx;width: 50rpx;"></image>
				<tui-text size="64" text="信息采集" style="margin-left: 20rpx;" @click="titleClick"></tui-text>
			</div>

		</tui-row>
		<tui-row style="height: 90%">
			<div style="display: flex;align-items: center;justify-content: center;width: 100%;height: 100vh; ">
				<image src="/static/clogo.png" style="height: 200rpx;width: 200rpx;"></image>
			</div>

		</tui-row>


		<tui-modal :show="equipmentInfoModalShow" style="z-index: 2;" custom>
			<view class="tui-modal-custom">
				<tui-row>
					<tui-form-item label="设备编号:">
						{{equipmentInfo.slaveCode}}
					</tui-form-item>
					<tui-form-item label="所属主机编号:">
						<tui-input type="primary" inputBorder v-model="equipmentInfo.equipmentCode"></tui-input>
					</tui-form-item>
				</tui-row>
				<tui-row>
					<tui-col :span="6" :offset="18" style="display: flex;">
						<tui-button type="primary" @click="saveEquipmentInfo">保存</tui-button>
						<tui-button plain style="margin-left: 20rpx;"
							@click="equipmentInfoModalShow=false">关闭</tui-button>
					</tui-col>
				</tui-row>


			</view>

		</tui-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				clickTimes: 0,
				equipmentInfoModalShow: false,
				equipmentInfo: {
					orgCode: "",
					appVersion: "",
					equipmentCode: "",
					slaveCode: ""
				}
			}
		},
		onLoad() {
			
			

		},
		onShow() {
			this.queryEquipInfo()
			this.timer = setInterval(() => {
				this.checkCameraStatus()
			}, 1000)
		},
		onHide() {
			this.stopCameraStatusCheck()
		},
		mounted() {

		},
		onUnload() {
			this.stopCameraStatusCheck()
		},
		methods: {
			titleClick: function() {
				this.clickTimes += 1;
				console.log("this clickTImes", this.clickTimes)
				if (this.clickTimes == 5) {
					this.showEquipmentInfo();
					this.clickTimes = 0
				}
			},
			showEquipmentInfo: function() {
				this.queryEquipInfo()
				this.equipmentInfoModalShow = true
			},
			queryEquipInfo: function() {
				let systemInfo = uni.getSystemInfoSync();
				this.equipmentInfo.slaveCode = systemInfo.deviceId;
				console.log(this.equipmentInfo.slaveCode)
				this.tui.request(
					"/app/equipment/slaveQueryEquipmentInfo",
					"GET", {
						slaveCode: this.equipmentInfo.slaveCode
					},
					false,
					false,
					false
				).then(res => {
					uni.hideToast()
					if (res.code == 200) {
						
						this.equipmentInfo.equipmentCode = res.data.equipmentCode
						uni.setStorageSync("equipmentCode", this.equipmentInfo.equipmentCode)
					}
				})
			},
			saveEquipmentInfo: function() {
				let postData = {
					equipmentCode: this.equipmentInfo.equipmentCode,
					slaveCode: this.equipmentInfo.slaveCode
				}
				this.tui.request(
					"/app/equipment/slaveSaveEquipmentInfo",
					"POST",
					postData,
					false,
					false,
					false
				).then(res => {
					uni.hideToast()
					if (res.code == 200) {
						uni.showToast({
							title: "信息保存成功！",
							icon: 'success'
						})
					}
				})
			},
			// 新增：检查摄像头状态的方法
			checkCameraStatus() {
				this.tui.requestSilenced(
					"/app/equipment/getCameraStatus", // 接口地址
					"GET", // 请求方法
					{
						equipmentCode: this.equipmentInfo.equipmentCode
					}, // 参数（根据实际接口需要添加）
					false,
					false,
					false
				).then(res => {
					if (res.code == 200) {
						
						let getData = res.data
						if (getData['open'] == true && getData['close'] == false) {
							this.stopCameraStatusCheck()
							uni.navigateTo({
								url: "/pages/tongueDiagnose/tongueDiagnose"
							})
						}
					}else {
						console.log("没有唤起摄像头, 返回内容是:", res)
					}
					
				}).catch(err => {
					console.error("摄像头状态请求失败:", err);
				});
			},
			// 停止轮询
			stopCameraStatusCheck() {
				if (this.timer) {
					clearInterval(this.timer);
					this.timer = null;
					console.log("已停止摄像头状态检查");
				}
			},
		}
	}
</script>

<style>

</style>